﻿
Partial Class pages_ebooks_generate
    Inherits CPage

    Public ReadOnly Property IsIframe As Boolean
        Get
            Return CWeb.RequestBool("iframe")
        End Get
    End Property
    Public ReadOnly Property ProductId As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
    Public ReadOnly Property Format As EFormat
        Get
            Return CWeb.RequestInt("format", EFormat.HTML)
        End Get
    End Property
    Protected Overrides Sub PageInit()
        RemoveSome()

        Dim list As CProductList = CProduct.Cache.HavingTranscripts()
        CDropdown.BlankItem(ddProduct, "-- Select a Product --")
        CDropdown.Add(ddProduct, CUtilities.NameAndCount("All Transcripts", list), 0)
        CDropdown.Add(ddProduct, "Inspirational Readings (hardcoded)", -1)
        For Each i As CProduct In list.BelongsTo
            Dim count As Integer = i.Children.HavingTranscripts.Count
            If count > 0 Then CDropdown.Add(ddProduct, CUtilities.NameAndCount(i.ProductName, count), i.ProductId)
        Next
        For Each i As CProduct In list.StandAlone
            CDropdown.Add(ddProduct, CUtilities.Truncate(i.ProductName), i.ProductId)
        Next
        CDropdown.SetValue(ddProduct, ProductId)

        CDropdown.AddEnums(ddFormat, GetType(EFormat))
        CDropdown.SetValue(ddFormat, Format)
    End Sub
    Protected Overrides Sub PagePreRender()
        Dim k As New CKindle(CDropdown.GetInt(ddFormat))

        If IsIframe Then
            If ProductId <> Integer.MinValue Then
                If -1 = ProductId Then
                    Response.Write(k.InspirationalReadings)
                Else
                    Response.Write(k.Display(ProductId))
                End If
            End If
            Response.End()
        Else
            Dim url As String = String.Concat("generate.aspx?iframe=true&productId=", ProductId, "&format=", CInt(Format))
            iframe.Attributes("src") = url
        End If
    End Sub

    Protected Sub btnGenerate_Click(sender As Object, e As System.EventArgs) Handles btnGenerate.Click
        Dim productId As Integer = CDropdown.GetInt(ddProduct)
        Dim p As CProduct = CProduct.Cache.GetById(productId)

        Dim name As String = CUtilities.NameAndCount("All", CProduct.Cache.HavingTranscripts)
        If Not IsNothing(p) Then name = p.ProductName.Replace("/", "").Replace("\", "") Else If productId = -1 Then name = "Inspirational Readings"

        Dim path As String = String.Concat(CConfig.DropboxArchive, "word\transcripts\", name, ".htm") 'Images are absolute path (html, smash)
        If Format = EFormat.EPUB OrElse Format = EFormat.MOBI OrElse Format = EFormat.SMASH Then 'Adjacent to images
            path = String.Concat(CConfig.DropboxPath, "\backups\live_data\uploads\products\", name.Replace(" ", "_"), ".htm")
        ElseIf Format = EFormat.PRINT Then
            path = String.Concat(CConfig.DropboxArchive, "\print\", name, ".htm")
        End If

        Dim k As New CKindle(Format)
        If productId = -1 Then
            IO.File.WriteAllText(path, k.InspirationalReadings, Encoding.UTF8)
        Else
            IO.File.WriteAllText(path, k.Display(p), Encoding.UTF8)
        End If

        lblOut.Text = String.Concat("Wrote '", name, "' (html) to: ", path.Replace(CConfig.DropboxPath, String.Empty))
    End Sub

    Private Sub RemoveSome()
        CKindle.INSP_READ.Remove(1174) 'Breakthru
        CKindle.INSP_READ.Remove(1175) 'breaking free
        CKindle.INSP_READ.Remove(1079) 'valiant men
    End Sub



    Protected Sub ddProduct_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddProduct.SelectedIndexChanged, ddFormat.SelectedIndexChanged
        Response.Redirect("generate.aspx?productId=" & CDropdown.GetInt(ddProduct) & "&format=" & CDropdown.GetInt(ddFormat))
    End Sub


End Class
